require 'ajax_scaffold'

class Sprint < ActiveRecord::Base

  has_many :tasks, :dependent => :destroy do
    # The sum of the initial estimates is the total_estimate
    def total_hours
      self.inject(0) { |res,t| res + t.initial_estimate }
      
    end
    
    def hours_left
      #100 - total_hours
      self.inject(0)  do |res,t|
        if (t.estimates.last != nil)
          res + t.estimates.last.hours
        end 
      end
        
    end
  
  end
  
  validates_presence_of :name, :goal
  validates_uniqueness_of :name
  validates_inclusion_of :number_of_days, :in => 1..99,
                         :message => 'must be a number between 1 and 99'    

  # The last date of the iteration
  def stop_date
    raise RuntimeError, "No start date provided"  unless !start_date.nil?
    start_date + number_of_days.to_i - 1
  end  
  
  def hours_per_day
    # I think there is an issue where you can update an estimate outside the "length" of the
    # sprint.
    
    hours_per_day = Array.new(number_of_days,0)
    self.tasks.each do |t|
      t.estimates.each do |e|
        # method with day
        #index = e.day
        #index = index -1
        
        # method with date
        index = e.date - self.start_date
        index = index -0
        #p "estimate: #{e.id}, #{e.date}, #{e.hours}, index: #{index}"
        hours_per_day[index] += e.hours
        #logger.info "From time(#{e.date}) to time (#{self.start_date}) #{e.date-self.start_date}"
      end
    end
    
    hours_per_day
  end
  

  def current?
    today = Time.now.at_midnight
    ( start_date.to_time <= today ) and ( stop_date.to_time >= today ) 
  end

  def future?
    tomorrow = Time.now.tomorrow.at_midnight
    start_date.to_time >= tomorrow
  end

  def past?
    today = Time.now.at_midnight
    stop_date.to_time < today
  end  
  
  def active? somedate
    active = false
    if (start_date <= somedate) and (somedate <= stop_date)
      tasks.each do |task|
        task.estimates.each do |estimate|
          if estimate.date == somedate
            active = true
            break
          end
        end
      end
    end
    active
  end
  
  def months_sprint_belongs_in
    #(start_date.month..stop_date.month).to_a
    sprint_months = []
    counterDate = Date.new(start_date.year,start_date.month, 1)
    number_of_months_sprint_lasts.downto(1) do 
      sprint_months << counterDate
      months = counterDate.month
      years = counterDate.year
      if months == 12
        months =1
        years += 1
      else
        months += 1
      end
      
      counterDate = Date.new(years,months, 1)
    end
    sprint_months
    
  end
  
  def number_of_months_sprint_lasts
    # logic pulled from http://www.bigbold.com/snippets/posts/show/2109
    # figure out how many months our sprint lasts.
    months = stop_date.month - start_date.month
    days = stop_date.day - start_date.day
    if months < 0
      months += 12
    end
    months += 1
  end
  
  def status
    status = nil
    if current?
      status = "in progress"
    end
    if future?
      status = "hasn't started"
    end
    if past?
      status = "finished"
    end
    
    status
  end
  
end
